package com.hosp.controller.v1;

import com.github.pagehelper.PageInfo;
import com.hosp.bean.Drug;
import com.hosp.controller.dto.DrugDTO;
import com.hosp.service.DrugService;
import com.hosp.vo.PageRequestVO;
import com.hosp.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 药品管理 API
 *
 * @author bing.zhang06@hand-china.com 2021/10/17 17:12
 **/
@Slf4j
@RestController
@RequestMapping("/drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    /**
     * 查询列表
     *
     * @param drug        查询条件
     * @param pageRequest 分页条件
     * @return 响应
     */
    @GetMapping("/list")
    public ResultVO<Drug> list(Drug drug, PageRequestVO pageRequest) {
        log.debug("input is {}.", drug);
        PageInfo<Drug> pageInfo = drugService.list(drug, pageRequest);
        return new ResultVO<>(pageInfo);
    }

    /**
     * 主键查询
     *
     * @param drId 查询条件
     * @return 响应
     */
    @GetMapping("/{drId}")
    public ResultVO<Drug> selectById(@PathVariable("drId") Integer drId) {
        Drug drug = drugService.selectById(drId);
        return new ResultVO<>(drug);
    }

    /**
     * 保存
     *
     * @param drugDTO 保存对象
     * @return 响应
     */
    @PostMapping("/save")
    public ResultVO<String> save(DrugDTO drugDTO) {
        log.debug("Drug is {}.", drugDTO);

        boolean flag = drugService.save(drugDTO);

        if (flag) {
            return new ResultVO<>(200, "保存成功");
        } else {
            return new ResultVO<>(500, "保存失败");
        }
    }
}
